package com.wang.bootbuilder.controller;

import com.wang.bootbuilder.annotation.HasPermission;
import com.wang.bootbuilder.entity.param.CategoryParam;
import com.wang.bootbuilder.service.CategoryService;
import com.wang.bootbuilder.tools.JsonResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/manage/category")
@Api(value = "/manage/category", description = "处理分类相关请求的controller")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    /*
    获取一级或某个二级分类列表
     */
    @GetMapping("/list")
    @HasPermission("2")
    public JsonResult getCategoryList(@RequestParam("parentId") Long parentId) {
        JsonResult jsonResult = categoryService.getCategoryList(parentId);
        return jsonResult;
    }


    /*
    添加分类
     */
    @PostMapping("/add")
    public JsonResult addCategory(@Validated @RequestBody CategoryParam param) {
        JsonResult jsonResult = categoryService.addCategory(param);
        return jsonResult;
    }

    /*
    更新品类名称
     */
    @PostMapping("/update")
    public JsonResult updateCategory(@Validated @RequestBody CategoryParam param) {
        JsonResult jsonResult = categoryService.updateCategoryName(param);
        return jsonResult;
    }

    /*
    根据分类ID获取分类
     */
    @GetMapping(value="/user/{categoryId}")
    public JsonResult getCategoryById(@PathVariable Long categoryId) {
        JsonResult jsonResult = categoryService.updateCategory(categoryId);
        return jsonResult;
    }

}
